package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.*;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.GoodsMapper;
import cn.wolfcode.car.business.mapper.GoodsStoreMapper;
import cn.wolfcode.car.business.query.GoodsQuery;
import cn.wolfcode.car.business.service.IGoodsService;
import cn.wolfcode.car.business.util.MinioUtils;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.base.query.QueryObject;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsStoreMapper goodsStoreMapper;

    @Override
    public TablePageInfo<Goods> query(GoodsQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Goods>(goodsMapper.selectForList(qo));
    }

    @Override
    public String uploading(MultipartFile file) throws Exception {

        String extension = FileUploadUtils.getExtension(file);
        if (!extension.equals("png")&&!extension.equals("jpg")){
            throw new BusinessException("请上传图片");
        }
        String path = MinioUtils.getUrl(file);
        return path;
    }

    @Override
    public void save(Goods goods) {
        goodsMapper.insert(goods);
    }

    @Override
    public String readResource(Long id) throws FileNotFoundException {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        File file = new File(goods.getGoodsCover());

        return goods.getGoodsCover();
    }

    @Override
    public Goods get(Long id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Goods goods) {
        goodsMapper.updateByPrimaryKey(goods);
    }

    @Override
    public void remove(Long id) {
        goodsMapper.deleteByPrimaryKey(id);
        goodsStoreMapper.deleteByGoodsId(id);
    }

    @Override
    public TablePageInfo<GoodsItem> goodsItem(Long id, QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<GoodsItem>(goodsMapper.selectGoodsItemById(id,qo));

    }


}
